package auth

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"tupu-go/controller/auth/req_res"
	"tupu-go/pkg/res"
	"tupu-go/service/auth"
)

type AuthController struct {
	authService *auth.AuthService
}

func NewAuthController() *AuthController {
	return &AuthController{
		authService: auth.NewAuthService(),
	}
}

func (l *AuthController) Login(c *gin.Context) {
	var req req_res.ReqLoginStruct
	if err := c.ShouldBindJSON(&req); err != nil {
		res.ErrorResponse(c, http.StatusBadRequest, err)
		return
	}
	// 调用业务层验证
	token, userInfo, err := l.authService.Login(c.Request.Context(), req)
	if err != nil {
		res.ErrorResponse(c, http.StatusInternalServerError, err)
		return
	}
	res.SuccessResponse(c, gin.H{"token": token, "userInfo": userInfo})
}
